package com.anlogic.sdk.ui;

import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.ui.actions.CloseUnrelatedProjectsAction;

import com.anlogic.sdk.utils.ProjectUtils;

public class CustomCloseUnrelatedProjectsAction extends CloseUnrelatedProjectsAction {

	public CustomCloseUnrelatedProjectsAction(IShellProvider provider) {
		super(provider);
	}

	@Override
	protected boolean updateSelection(IStructuredSelection s) {
		return super.updateSelection(s) && !isInDebug();
	}

	protected boolean isInDebug() {
		List<? extends IResource> projects = getSelectedResources();
		if (projects == null || projects.isEmpty()) {
			return false;
		}
		
		final IResource[] projectArray = projects.toArray(new IResource[projects.size()]);
		
		for (IResource element : projectArray) {
			IProject project = (IProject) element;
			if (ProjectUtils.isDebugging(project)) {
				return true;
			}
		}
	
		return false;
	}
}
